package com.macro.mall.controller;

import com.macro.mall.common.api.CommonPage;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.model.CurVideo;
import com.macro.mall.service.CurVideoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
* 视频表(cur_video）
* @author gaodx
* @create 2020-08-11 15:43
**/
@Slf4j
@RestController
@RequestMapping("curVideo")
@Api(tags = "CurVideoController", description = "视频表(cur_video）")
public class CurVideoController {
    @Autowired
    CurVideoService curVideoService;

    @ApiOperation("查询视频列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<CurVideo>> list(@RequestParam(value = "sourceId", defaultValue = "1") Integer sourceId,
                                                   @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                   @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize
                                                        ) {


        List<CurVideo> curVideos = curVideoService.find(sourceId, pageNum, pageSize);
        return CommonResult.success(CommonPage.restPage(curVideos));
    }

    @ApiOperation("添加视频")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult save(@RequestBody CurVideo request) {
        try {
            curVideoService.saveNotNull(request);
            return CommonResult.success(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return CommonResult.failed();
    }


    @ApiOperation("删除")
    @RequestMapping(value = "/del", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult del(@ApiParam(value = "视频id") @RequestParam(value = "id", required = false) Long id,
                            @ApiParam(value = "课程id") @RequestParam(value = "courseId", required = false) Long courseId) {
        try {
            curVideoService.deleteById(id,courseId);
            return CommonResult.success(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return CommonResult.failed();
    }

    @ApiOperation("修改视频信息")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult update(@RequestBody CurVideo request) {
        try {
            curVideoService.updateNotNullById(request);
            return CommonResult.success(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return CommonResult.failed();
    }
    @ApiOperation("查询视频详情")
    @RequestMapping(value = "/getById", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult getById(@RequestParam(value = "id", required = false) Long id) {
        try {
            CurVideo curCourseType = curVideoService.findOne(id);
            return CommonResult.success(curCourseType);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return CommonResult.failed();
    }

 /*   @ApiOperation("查询业务范围")
    @RequestMapping(value = "/getById", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult getById(@RequestParam(value = "id", required = false) String id) {
        try {
            CurCourseType curCourseType = curVideoService.selectCurCourseTypeById(id);
            return CommonResult.success(curCourseType);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return CommonResult.failed();
    }

    @ApiOperation("修改业务范围")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult update(@RequestBody CurCourseType request) {
        try {
            curCourseTypeService.updateCurCourseTypeSelective(request);
            return CommonResult.success(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return CommonResult.failed();
    }*/

}